package com.example.bottomnavigationdemo.ui.b_dashboard

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.example.bottomnavigationdemo.R
import com.just.agentweb.AgentWeb

class DashboardFragment : Fragment() {

    private lateinit var dashboardViewModel: DashboardViewModel
    var mAgentWeb: AgentWeb? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.e("-------->", "DashboardFragment-onCreate")

    }

    override fun onDestroy() {
        super.onDestroy()
        Log.e("-------->", "DashboardFragment-onDestroy")
    }
    override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
    ): View? {
//        Log.e("-------->", "onCreateView")
        dashboardViewModel =
                ViewModelProviders.of(this).get(DashboardViewModel::class.java)
        val root = inflater.inflate(R.layout.fragment_dashboard, container, false)
        val textView: TextView = root.findViewById(R.id.text_dashboard)
        dashboardViewModel.text.observe(viewLifecycleOwner, Observer {
            textView.text = it
        })

        mAgentWeb = AgentWeb.with(this)
            .setAgentWebParent(root as ConstraintLayout, ConstraintLayout.LayoutParams(-1, -1))
            .useDefaultIndicator()
//            .setWebViewClient()
            .interceptUnkownUrl()
            .createAgentWeb()
            .ready()
            .go("https://www.jd.com/")
//        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
//            mAgentWeb?.webCreator?.webView?.webViewClient = object : WebViewClient() {
//                override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
//                    //  XLog.e("测试～～～ ${request?.url.toString()}")
//                    return when {
//                        request?.url.toString().endsWith(".apk") -> true
//                        request?.url.toString().startsWith("tel:") -> true
//                        request?.url.toString().startsWith("mailto:") -> true
//                        request?.url.toString().startsWith("baidu") -> true
//                        else -> super.shouldOverrideUrlLoading(view, request)
//                    }
//                }
//
//                override fun onPageFinished(view: WebView?, url: String?) {
//                    super.onPageFinished(view, url)
////                    XLog.e("测试～～～ ${url}")
////                    mAgentWeb?.getJsAccessEntrace()?.quickCallJs("loadLocalHtmlWithType", "1")
//                }
//            }
//        }


        return root
    }
}